package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuEquipmentEntity;
import io.gbznt.modules.buss.entity.BussRtuWarningRecordEntity;
import io.gbznt.modules.buss.entity.BussRtuWarningRoleEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuEquipmentService;
import io.gbznt.modules.buss.service.BussRtuRecordService;
import io.gbznt.modules.buss.service.BussRtuWarningRecordService;
import io.gbznt.modules.buss.service.BussRtuWarningRoleService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import net.sf.json.JSONObject;

/**
 * RTU告警记录
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:34
 */
@RestController
@RequestMapping("buss/bussrtuwarningrecord")
public class BussRtuWarningRecordController {
	@Autowired
	private BussRtuWarningRecordService bussRtuWarningRecordService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private BussFarmlandInfoService bussFarmlandInfoService;
	@Autowired
	private BussRtuEquipmentService bussRtuEquipmentService;
	@Autowired
	private BussRtuWarningRoleService bussRtuWarningRoleService;
	@Autowired
	private BussRtuRecordService bussRtuRecordService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("buss:bussrtuwarningrecord:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = bussRtuWarningRecordService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("buss:bussrtuwarningrecord:info")
	public R info(@PathVariable("id") String id) {
		BussRtuWarningRecordEntity bussRtuWarningRecord = bussRtuWarningRecordService.getById(id);
		if (StringUtils.isNotBlank(bussRtuWarningRecord.getDeptId())) {// 部门
			SysDeptEntity dept = sysDeptService.getById(bussRtuWarningRecord.getDeptId());
			if (dept != null && StringUtils.isNotBlank(dept.getName())) {
				bussRtuWarningRecord.setDeptName(dept.getName());
			}
		}
		if (StringUtils.isNotBlank(bussRtuWarningRecord.getFarmlandInfoId())) {// 田块
			BussFarmlandInfoEntity bussFarmlandInfo = bussFarmlandInfoService
					.getById(bussRtuWarningRecord.getFarmlandInfoId());
			if (bussFarmlandInfo != null && StringUtils.isNotBlank(bussFarmlandInfo.getName())) {
				bussRtuWarningRecord.setTkName(bussFarmlandInfo.getName());
			}
		}
		if (StringUtils.isNotBlank(bussRtuWarningRecord.getEquipmentId())) {// 设备
			BussRtuEquipmentEntity bussRtuEquipment = bussRtuEquipmentService
					.getById(bussRtuWarningRecord.getEquipmentId());
			if (bussRtuEquipment != null && StringUtils.isNotBlank(bussRtuEquipment.getName())) {
				bussRtuWarningRecord.setEquipmentName(bussRtuEquipment.getName());
			}
		}
		if (StringUtils.isNotBlank(bussRtuWarningRecord.getWarningRoleId())) {// 预警规则
			BussRtuWarningRoleEntity bussRtuWarningRole = bussRtuWarningRoleService
					.getById(bussRtuWarningRecord.getWarningRoleId());
			if (bussRtuWarningRole != null && StringUtils.isNotBlank(bussRtuWarningRole.getName())) {
				bussRtuWarningRecord.setWarningRoleName(bussRtuWarningRole.getName());
			}
		}

		return R.ok().put("bussRtuWarningRecord", bussRtuWarningRecord);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("buss:bussrtuwarningrecord:save")
	public R save(@RequestBody BussRtuWarningRecordEntity bussRtuWarningRecord) {
		ValidatorUtils.validateEntity(bussRtuWarningRecord, AddGroup.class);

		bussRtuWarningRecord.setCreateId(ShiroUtils.getUserId() + "");
		bussRtuWarningRecord.setCreateTime(new Date());
		bussRtuWarningRecordService.save(bussRtuWarningRecord);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("buss:bussrtuwarningrecord:update")
	public R update(@RequestBody BussRtuWarningRecordEntity bussRtuWarningRecord) {
		ValidatorUtils.validateEntity(bussRtuWarningRecord, AddGroup.class);

		bussRtuWarningRecord.setUpdateId(ShiroUtils.getUserId() + "");
		bussRtuWarningRecord.setUpdateTime(new Date());
		Boolean re = bussRtuWarningRecordService.updateById(bussRtuWarningRecord);// 全部更新

		return re ? R.ok() : R.oldVersion();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("buss:bussrtuwarningrecord:delete")
	public R delete(@RequestBody String[] ids) {
		bussRtuWarningRecordService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 * 页面上的生成工单按钮
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping("/addGongDan")
	@RequiresPermissions("buss:bussmaintaintask:save")
	public R addGongDan(@RequestParam Map<String, Object> params) {
		// 告警记录id
		String id = (String) params.get("id");
		BussRtuWarningRecordEntity bussRtuWarningRecord = bussRtuWarningRecordService.getById(id);
		// json数据集
		JSONObject json = JSONObject.fromObject(bussRtuWarningRecord.getGjjlJson());
		// 字典id
		String zdId = (String) json.get("id");

		if (bussRtuWarningRecord.getWarningType() == 4) {
			bussRtuRecordService.addGdByGjjl(bussRtuWarningRecord, zdId, 1);
		} else {
			bussRtuRecordService.addGdByGjjl(bussRtuWarningRecord, zdId, 0);
		}

		System.out.println(json.get("id"));

		return R.ok();
	}

}
